package com.itheima.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public PageBean page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());

        List<Clazz> list = clazzMapper.list(empQueryParam);
        LocalDate now = LocalDate.now();
        for (Clazz clazz : list) {
            if(now.toString().compareTo(clazz.getEndDate().toString())>0){
                clazz.setStatus("已结课");
            }else if(now.toString().compareTo(clazz.getBeginDate().toString())>0){
                clazz.setStatus("已开班");
            }else {
                clazz.setStatus("未开班");
            }
        }
        Page<Clazz> p = (Page<Clazz>) list;
        return new PageBean(p.getTotal(),p.getResult());
    }



    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());

        clazzMapper.insert(clazz);
    }



    @Override
    public Clazz getInfo(Integer id) {
        Clazz clazz = clazzMapper.getInfo(id);

        return clazz;
    }


    @Override
    public void modify(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());

        clazzMapper.modify(clazz);
    }

    @Override
    public List<Clazz> getAll() {
        List<Clazz> list = clazzMapper.getAll();
        return list;
    }

    @Override
    public Result delete(Integer id) {
        Integer count = clazzMapper.getCount(id);
        if(count>0){
            return Result.error("哥们下面有人,删不了");
        }
        clazzMapper.delete(id);
        return Result.success();
    }

//    @Override
//    public Integer getCount(Integer id) {
//       return clazzMapper.getCount(id);
//    }


}
